package com.qzsoft.system.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 统一返回结果封装类
 *
 * @author gbx
 * @since 2020-08-27 15:56:07
 */
@ApiModel(description = "返回结果")
public class Result<T> {
    @ApiModelProperty(
            value = "状态码 默认值 0",
            required = true,
            example = "0",
            position = 1
    )
    private int code;
    @ApiModelProperty(
            value = "返回信息 默认值 成功",
            required = true,
            example = "成功",
            position = 2
    )
    private String msg;
    @ApiModelProperty(
            value = "返回数据",
            position = 3
    )
    private T data;

    public Result() {
        this.code = 0;
        this.msg = "成功";
    }

    public Result(T data, String msg, int code) {
        this.code = 0;
        this.msg = "成功";
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public Result(T data) {
        this(data, "成功", 0);
    }

    public Result(T data, Result.Status status) {
        this(data, status.getMsg(), status.getCode());
    }

    public Result(T data, String customMsg, Result.Status status) {
        this(data, customMsg, status.getCode());
    }

    public static <T> Result<T> ok(T data) {
        return new Result(data);
    }

    public static <T> Result<T> ok() {
        return new Result((Object) null);
    }

    public boolean isOK() {
        return this.code == 0;
    }

    public static <T> Result<T> badRequest(String msg, int code) {
        return new Result((Object) null, msg, code);
    }

    public static <T> Result<T> badRequest(Result.Status status) {
        return new Result((Object) null, status);
    }

    public static <T> Result<T> badRequest(Result.Status status, String customMsg) {
        return new Result((Object) null, String.format("%s;%s", status.getMsg(), customMsg), status.getCode());
    }

    public static <T> Result<T> badRequest() {
        return new Result((Object) null, Result.Status.BAD_REQUEST);
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return (new StringBuilder(500)).append("Result{code='").append(this.code).append("',msg='").append(this.msg).append("',data='").append(this.data).append("'}").toString();
    }

    public static enum Status {
        OK(0, "成功"),
        BAD_REQUEST(400, "无效请求"),
        BAD_PARAMETER(40001, "参数无效"),
        UNAUTHORIZED(401, "无权限"),
        FORBIDDEN(403, "资源禁止访问"),
        METHOD_NOT_ALLOWED(405, "方法不允许访问"),
        CONFLICT(409, "请求冲突"),
        INTERNAL_SERVER_ERROR(500, "服务异常"),
        UNKNOWN_ERROR(50001, "未知错误"),
        BAD_GATEWAY(502, "无效网关"),
        SERVICE_UNAVAILABLE(503, "服务不可用"),
        GATEWAY_TIMEOUT(504, "网关超时");

        private final int code;
        private final String msg;

        private Status(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return this.code;
        }

        public String getMsg() {
            return this.msg;
        }

        public static Result.Status valueOf(int code) {
            Result.Status[] var1 = values();
            int var2 = var1.length;

            for (int var3 = 0; var3 < var2; ++var3) {
                Result.Status val = var1[var3];
                if (val.code == code) {
                    return val;
                }
            }

            return UNKNOWN_ERROR;
        }
    }
}
